﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace QBS.Tools
{
    public class SearchData
    {
        private HttpRequest _request;
        public SearchData(HttpRequest request)
        {
            _request = request;
        }
        public DateTime? beg { get; set; }
        public DateTime? end { get; set; }
        public string keyword { get; set; }
        public int user { get; set; }
        public int member { get; set; }
        public int area { get; set; }
        public int sort { get; set; }
        public int status { get; set; }
        public int lv { get; set; }
        public int role { get; set; }
        public int sector { get; set; }
        public int TryInt(string key, int def = -1)
        {
            if (Empty(key))
                return def;
            return _request[key].ToInt();
        }
        public long TryLong(string key, long def = -1)
        {
            if (Empty(key))
                return def;
            return _request[key].ToLong();
        }
        public bool Empty(string key)
        {
            return _request[key].IsEmpty();
        }
        public string ToStr(string key)
        {
            return _request[key];
        }


    }
    public class Pager
    {
        private HttpRequest request
        {
            get
            {
                return HttpContext.Current.Request;
            }
        }
        private HttpSessionState session
        {
            get
            {
                return HttpContext.Current.Session;
            }
        }
        public Pager(int size)
        {
            _pagesize = size;
        }
        private string _sessionKey;
        public Pager()
        {
            try
            {
                _sessionKey = HttpContext.Current.Request.FilePath;
            }
            catch
            {
                _sessionKey = "";
            }
            Total = 0;
        }
        private int _pagesize;
        public int PageSize
        {
            get
            {
                if (request["size"].ToInt() > 0)
                    _pagesize = request["size"].ToInt();
                return _pagesize;
            }
            set { _pagesize = value; }
        }
        public int PageNumShow { get; set; }

        public string limit
        {
            get
            {
                int total = PageIndex * PageSize;
                return string.Format(" limit {0},{1}", total, PageSize);
            }
        }

        public string orderBy(string field = "id", bool desc = true)
        {
            string s = desc ? " desc " : " asc ";
            return string.Format(" order by {0} {1}", field, s);
        }
        public string WeixinNumPageHtml
        {
            get
            {
                string queryStr = "?";
                for (int i = 0; i < request.QueryString.Count; i++)
                {
                    string key = request.QueryString.Keys[i];
                    if (key != "page" && !key.IsEmpty())
                        queryStr += key + "=" + request.QueryString[key] + "&";
                }
                queryStr += "page=";
                int pageTotal = Total / PageSize;
                if (Total % PageSize > 0)
                {
                    pageTotal = pageTotal + 1;
                }

                if (PageIndex > pageTotal) { _page = pageTotal; }
                string html = "<ul class=\"pagination pull-right\">";
                if (PageIndex > 1)
                    html += "<li><a href=\"" + queryStr + (PageIndex - 1) + "\">上一页</i></a></li>";
                else
                    html += "<li class=\"disabled\"><a href=\"#\">上一页</a></li>";

                int tmpIndex = 0;
                //if (Page < 10 && Page % 10 == 0)
                //    tmpIndex++;
                //tmpIndex = tmpIndex * 10;
                if (PageIndex > 5)
                    tmpIndex = PageIndex - 5;

                for (int i = 1; i < 10; i++)
                {
                    tmpIndex++;
                    if (pageTotal < tmpIndex)
                        break;
                    html += "<li class=\"" + (tmpIndex == PageIndex ? "active" : "") + "\"><a href=\"" + queryStr + tmpIndex + "\">" + tmpIndex + "</a></li>";
                }
                if (pageTotal > PageIndex)
                    html += "<li><a href=\"" + queryStr + (PageIndex + 1) + "\">下一页</i></a></li>";
                else
                    html += "<li class=\"disabled\"><a href=\"#\">下一页</a></li>";
                html += "</ul>";
                return html;
            }
        }
        private int _page;
        public string NumPageHtml
        {
            get
            {
                string queryStr = "?";
                for (int i = 0; i < request.QueryString.Count; i++)
                {
                    string key = request.QueryString.Keys[i];
                    if (key != "page" && !key.IsEmpty())
                        queryStr += key + "=" + request.QueryString[key] + "&";
                }
                queryStr += "page=";
                int pageTotal = Total / PageSize;
                if (Total % PageSize > 0)
                {
                    pageTotal = pageTotal + 1;
                }

                if (PageIndex > pageTotal) { _page = pageTotal; }
                string html = "<ul class=\"pagination pull-right\"><li><a>共用[ " + Total + " ]条数据,分[ " + pageTotal + " ]页,每页[ " + PageSize + " ]条数据</a></li>";
                if (PageIndex > 1)
                    html += "<li><a href=\"" + queryStr + (PageIndex - 1) + "\">上一页</i></a></li>";
                else
                    html += "<li class=\"disabled\"><a href=\"#\">上一页</a></li>";

                int tmpIndex = 0;
                //if (Page < 10 && Page % 10 == 0)
                //    tmpIndex++;
                //tmpIndex = tmpIndex * 10;
                if (PageIndex > 5)
                    tmpIndex = PageIndex - 5;

                for (int i = 1; i < 10; i++)
                {
                    tmpIndex++;
                    if (pageTotal < tmpIndex)
                        break;
                    html += "<li class=\"" + (tmpIndex == PageIndex ? "active" : "") + "\"><a href=\"" + queryStr + tmpIndex + "\">" + tmpIndex + "</a></li>";
                }
                if (pageTotal > PageIndex)
                    html += "<li><a href=\"" + queryStr + (PageIndex + 1) + "\">下一页</i></a></li>";
                else
                    html += "<li class=\"disabled\"><a href=\"#\">下一页</a></li>";
                html += "</ul>";
                return html;
            }
        }
        public int PageIndex
        {
            get
            {
                if (_page == 0)
                    _page = request["page"].ToInt();
                if (_page == 0)
                    _page = 1;
                _page--;
                return _page;
            }
        }
        public int Total { get; set; }

        public NameValueCollection QueryString
        {
            get;
            set;
        }
        private SearchData _search;

        #region 搜索框关键字收集
        public SearchData Search
        {
            get
            {
                if (_search == null)
                {
                    _search = new SearchData(request);
                    #region 取日期搜索
                    if (!_search.ToStr("search_beg").IsEmpty())
                        _search.beg = _search.ToStr("search_beg").ToDate();
                    if (!_search.ToStr("search_end").IsEmpty())
                        _search.end = _search.ToStr("search_end").ToDate();
                    #endregion

                    _search.keyword = _search.ToStr("search_keyword");
                    _search.user = _search.TryInt("search_user");
                    _search.member = _search.TryInt("search_member");
                    _search.area = _search.TryInt("search_area");
                    _search.sort = _search.TryInt("search_sort");
                    _search.status = _search.TryInt("search_status");

                    _search.lv = _search.TryInt("search_lv");
                    _search.role = _search.TryInt("search_role");
                    _search.sector = _search.TryInt("search_sector");
                }
                return _search;
            }
        }
        #endregion
    }
}
